<template>
  <div class="history-container">
    <h3>健康检测历史记录</h3>
    <el-card v-for="item in historyList" :key="item.id" class="history-item">
      <div class="history-header">
        <span class="check-type">{{ item.recordType }}</span>
        <span class="date">{{ formatDate(item.createTime) }}</span>
      </div>
      <div class="history-content">
        <p><strong>摘要/结果：</strong>{{ item.content }}</p>
        <p><strong>备注：</strong>{{ item.notes }}</p>
      </div>
      <div class="history-footer">
        <el-button type="primary" size="small" @click="viewDetail(item.id)">查看详情</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getHealthHistoryApi } from '../api/healthcheck'

const router = useRouter()
const historyList = ref([])

const formatDate = date => new Date(date).toLocaleDateString()

const viewDetail = id => {
  router.push(`/healthcheck/detail/${id}`)
}

const loadHistory = async () => {
  try {
    const res = await getHealthHistoryApi()
    historyList.value = res.data
  } catch (error) {
    console.error('获取历史记录失败:', error)
    ElMessage.error('获取历史记录失败')
  }
}

onMounted(loadHistory)
</script>

<style scoped>
.history-container {
  padding: 16px;
  max-width: 800px;
  margin: 0 auto;
}

.history-item {
  margin-bottom: 16px;
}

.history-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 12px;
}

.check-type {
  font-size: 16px;
  font-weight: bold;
  color: #409EFF;
}

.date {
  color: #909399;
  font-size: 14px;
}

.history-content {
  margin: 12px 0;
  line-height: 1.6;
}

.history-content p {
  margin: 8px 0;
}

.history-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 12px;
}
</style>
